<?php

namespace app\admin\controller;

use think\Controller;
use app\admin\model\SiteNode;
use think\Request;

class SiteNodes extends Controller
{
    /***加载首页
     * @return \think\response\View
     * @throws \think\exception\DbException
     */
    public function index()
    {
        $nodes = SiteNode::all();
//        return $nodes;

        return view('SiteNodes/index', compact('nodes'));
    }

    /**加载新增页面
     * @return \think\response\View
     */
    public function create()
    {
        return view('SiteNodes/create');
    }

    /***保存新增
     * @param Request $request
     * @return \think\response\Redirect
     */
    public function save(Request $request)
    {
        SiteNode::create($request->param());
        return redirect('SiteNodes/index');
    }

    /***加载编辑页面
     * @param $id
     * @return \think\response\View
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function edit($id)
    {
        $node = SiteNode::find($id);
        return view('SiteNodes/edit', compact('node'));
    }

    /***保存编辑
     * @param Request $request
     * @param $id
     * @return \think\response\Redirect
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function update(Request $request, $id)
    {
        $node = SiteNode::find($id);
        $node->allowField(true)->save($request->param(), ['id' => $id]);
        return redirect('SiteNodes/index');

    }

    /***删除
     * @param $id
     * @return \think\response\Redirect
     */
    public function delete($id)
    {
        SiteNode::destroy($id);
        return redirect('SiteNodes/index');
    }
}